package de.haw.shc.Szenario;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ToggleButton;
import de.haw.shc.Action.Action_Impl;
import de.haw.shc.Config.XML_Reader;
import de.haw.shc.Interfaces.DeviceProperty.Device;
import de.haw.shc.Interfaces.DeviceProperty.iColorLight;
import de.haw.shc.JSONObjects.Blinds;
import de.haw.shc.JSONObjects.Curtain;
import de.haw.shc.JSONObjects.Light;
import de.haw.shc.JSONObjects.Szene;
import de.haw.shc.R;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: mizus
 * Date: 29.06.13
 * Time: 16:29
 * To change this template use File | Settings | File Templates.
 */

public class LoveSzenario extends Fragment {

    private Map<String, List<Device>> roomMap;
    private playerThread watchThread;
    private ToggleButton dinner_btn, sleeping_btn, lounge_btn;


    public LoveSzenario(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View tView = inflater.inflate(R.layout.szenario_love, container, false);

        dinner_btn = (ToggleButton) tView.findViewById(R.id.btn_act_dinner);
        sleeping_btn = (ToggleButton) tView.findViewById(R.id.btn_act_sleep);
        lounge_btn =  (ToggleButton) tView.findViewById(R.id.btn_act_lounge);


        XML_Reader xml_reader = XML_Reader.createXMLReader(getResources());
        roomMap = xml_reader.getRaumMap();

        watchThread = new playerThread();

//       if (savedInstanceState  == null || savedInstanceState.get("Data") == null) {
//           dataToSave = new DataToSave();
//            dataToSave.setBtn_dinner(dinner_btn);
//            dataToSave.setBtn_lounge(lounge_btn);
//            dataToSave.setBtn_sleep(sleeping_btn);
//            Log.d("DATAtoSave", "========>" + dataToSave);
//        } else   {
//            Log.d("DATAtoSave", "========> else " + dataToSave);
//            dataToSave = (DataToSave) savedInstanceState.get("Data");
//            dinner_btn.setChecked(dataToSave.getBtn_dinner().isChecked());
//            lounge_btn.setChecked(dataToSave.getBtn_lounge().isChecked());
//            sleeping_btn.setChecked(dataToSave.getBtn_sleep().isChecked());
//        }




        dinner_btn = (ToggleButton) tView.findViewById(R.id.btn_act_dinner);
        sleeping_btn = (ToggleButton) tView.findViewById(R.id.btn_act_sleep);
        lounge_btn =  (ToggleButton) tView.findViewById(R.id.btn_act_lounge);
        lounge_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Device> roomList = roomMap.get(getString(R.string.lounge));
                if(lounge_btn.isChecked()){
                    watchThread.setPause(false);
                    action_music_love();
                    lightoffLove(roomList);
                    down(roomList, ONLY_CURTAIN);
                }  else {
                    lightOFF(roomList);
                    openBlinds(roomList);
                    if (!isAllOff()) {
                        watchThread.setPause(true);
                        flurlicht_An();
                        watchThread.getMediaPlayer().stop();
                    }
                }
            }
        });


        dinner_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Device> roomList = roomMap.get(getString(R.string.dinner));
                Szene dinnerSzene = new Szene(getString(R.string.szene_romanticDinner));
                if(dinner_btn.isChecked()){
                    watchThread.setPause(false);
                    action_music_love();
                    lightForDinner(roomList);
                    Action_Impl.getInstance().iniAction(dinnerSzene.startSzene(), dinnerSzene.getDeviceControll());
                }  else {
                    lightOFF(roomList);
                    openBlinds(roomList);
                    Action_Impl.getInstance().iniAction(dinnerSzene.stopSzene(), dinnerSzene.getDeviceControll());
                    if (!isAllOff()){
                        watchThread.setPause(true);
                        flurlicht_An();
                        watchThread.getMediaPlayer().stop();
                    }
                }
            }
        });


        sleeping_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Device> roomList = roomMap.get(getString(R.string.sleep));
                if(sleeping_btn.isChecked()){
                    watchThread.setPause(false);
                    action_music_love();
                    lightoffLove(roomList);
                    down(roomList, BOTH);
                }  else {
                    openBlinds(roomList);
                    lightOFF(roomList);
                    if (!isAllOff()){
                        watchThread.setPause(true);
                        flurlicht_An();
                        watchThread.getMediaPlayer().stop();
                    }
                }
            }
        });
        return tView;
    }

    private boolean isAllOff(){
        return  dinner_btn.isChecked() || sleeping_btn.isChecked() || lounge_btn.isChecked();
    }



    private void lightOFF(List<Device> aList){
        for (Device elem: aList){
            if(elem.isLight()){
                iColorLight light = (iColorLight)elem;
                Action_Impl.getInstance().iniAction(light.createOffColorLight(), light.getDeviceControll());
                Action_Impl.getInstance().iniAction(light.createLightOff(), light.getDeviceControll());
            }

            Action_Impl.getInstance().iniAction(elem.deactivate(), elem.getDeviceControll());
        }
    }

    private final int ONLY_BLINDS = 0;
    private final int ONLY_CURTAIN = 1;
    private final int BOTH = 2;

    private void down(List<Device> list, int Typ){
        for(Device elem: list){
          switch (Typ){
              case ONLY_BLINDS:
                  downBlind(elem);
                  break;
              case ONLY_CURTAIN:
                  downCurtain(elem);
                  break;
              case BOTH:
                  downCurtain(elem);
                  downBlind(elem);
                  break;
          }
        }
    }

    private void downBlind(Device elem){
        if(elem.isBlind()) {
            Blinds blinds = (Blinds)elem;
            Action_Impl.getInstance().iniAction(blinds.createAllBlindsClose(), blinds.getDeviceControll());
        }
    }

    private void downCurtain(Device elem){
        if (elem.isCurtain()){
            Curtain curtain = (Curtain) elem;
            Action_Impl.getInstance().iniAction(curtain.createCloseCurtain(), curtain.getDeviceControll());
        }
    }


    private void openBlinds(List<Device> list){
        for (Device elem : list){
            if (elem.isBlind()){
                Blinds blinds = (Blinds) elem;
                Action_Impl.getInstance().iniAction(blinds.createOpenBlinds(), blinds.getDeviceControll());
            }
        }
    }


    private void lightoffLove(List<Device> list){
        for (Device elem: list){
            if (elem.isLight()){
                Light light = (Light)elem;
                Action_Impl.getInstance().iniAction(light.createColorLight(240, 0,0,0), light.getDeviceControll());
            }
        }
    }

    private void lightForDinner(List<Device> list){
        for (Device elem: list){
            if (elem.isLight()){
                Light light = (Light)elem;
                Action_Impl.getInstance().iniAction(light.createLightOn(), light.getDeviceControll());
            }
        }
    }


    private void flurlicht_An(){
        List<Device> deviceList = roomMap.get(getString(R.string.corridor));
        for(Device elem: deviceList){
            if (elem.isLight()){
                Light light = (Light)elem;
                Action_Impl.getInstance().iniAction(light.createLightOn(), light.getDeviceControll());
            }
        }
    }


    public void onDestroy(){
        if (watchThread.getMediaPlayer() != null)watchThread.getMediaPlayer().stop();
        watchThread.interrupt();
        super.onDestroy();
    }



    void action_music_love(){
            try{
               watchThread.start();
            } catch (IllegalThreadStateException e){
                 Log.d("ThreadStateException", e.getMessage());
            }
    }

    public class playerThread extends Thread{

        private MediaPlayer mediaPlayer;
        private boolean pause = false;

        public playerThread(){
        }


        public synchronized MediaPlayer getMediaPlayer(){
            return mediaPlayer;
        }


        public void run(){
                while(!isInterrupted()){
                    if (!pause){
                        if (mediaPlayer == null || !mediaPlayer.isPlaying()){
                            mediaPlayer = MediaPlayer.create(getActivity(), R.raw.love_mode);
                            mediaPlayer.start();
                        }
                    }
                }
        }

        public synchronized void setPause(boolean aPause){
            pause = aPause;
        }



    }




}
